﻿Imports System.Data.SqlClient

Public Class frmTarjetas
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal

    Private Sub frmTarjetas_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró el Catálogo de tarjetas de proximidad")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmTarjetas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        Inicia()
        CargaTarjetas()
        CargaOperadores()

        Bitacora.RegistraActividad("Ingresó al Catálogo de tarjetas de proximidad")
        Usuario.Ubicacion("Catálogo de Tarjetas")
    End Sub
    Sub Inicia()
        txtTarjeta.Clear()
        datInicio.Value = Now()
        datTermino.Value = Now()
        txtOperador.Clear()
        txtTarjeta.Focus()

    End Sub
    Sub CargaTarjetas()
        dgTarjetas.Rows.Clear()

        Dim cnconn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select CodigoTarjeta,InicioVigencia,TerminoVigencia,Nombre,Status FROM Tarjetas T INNER JOIN OperadoresGlobal O ON T.IdOperador=O.IdOperador ORDER BY CodigoTarjeta"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim Inicio As Date
            Dim Termino As Date
            Dim Status As String

            Do While rdBuscar.Read()
                Inicio = rdBuscar("InicioVigencia")
                Termino = rdBuscar("TerminoVigencia")
                If rdBuscar("Status") = 1 Then
                    Status = "Activa"
                Else
                    Status = "Inactiva"
                End If
                dgTarjetas.Rows.Add(rdBuscar("CodigoTarjeta").ToString.Trim, rdBuscar("Nombre").ToString.Trim, Inicio.ToShortDateString, Termino.ToShortDateString, Status)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub
    Sub CargaOperadores()
        txtOperador.AutoCompleteCustomSource = Nothing

        Dim cnconn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select Nombre FROM OperadoresGlobal WHERE EstadoEmpleado<>'B'"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader
            Do While rdBuscar.Read()
                txtOperador.AutoCompleteCustomSource.Add(rdBuscar("Nombre").ToString.Trim)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim Status As Integer = BuscarIntDatoCitra("SELECT Status FROM Tarjetas WHERE CodigoTarjeta='" & dgTarjetas.CurrentRow.Cells(0).Value & "'")
        If Status = 0 Then
            MsgBox("No se puede eliminar una tarjeta que esta cancelada", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        Call SetSqlCitra("DELETE Tarjetas WHERE CodigoTarjeta='" & dgTarjetas.CurrentRow.Cells(0).Value & "'")
        CargaTarjetas()
        Bitacora.RegistraActividad("Eliminó la tarjeta " & dgTarjetas.CurrentRow.Cells(0).Value)
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim Status As Integer = BuscarIntDatoCitra("SELECT Status FROM Tarjetas WHERE CodigoTarjeta='" & dgTarjetas.CurrentRow.Cells(0).Value & "'")
        If Status = 0 Then
            MsgBox("La tarjeta ya se encuentra cancelada", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim Resp As Integer = MsgBox("La tarjeta quedara inutilizable, ¿Desea cancelarla?", MsgBoxStyle.YesNo, "Aviso")
        If Resp = 6 Then
            Call SetSqlCitra("UPDATE Tarjetas SET Status=0 WHERE CodigoTarjeta='" & dgTarjetas.CurrentRow.Cells(0).Value & "'")
            CargaTarjetas()
            Bitacora.RegistraActividad("Canceló la tarjeta " & dgTarjetas.CurrentRow.Cells(0).Value)
        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        'Verifica Vigencia
        If datTermino.Value < datInicio.Value Then
            MsgBox("La vigencia es incorrecta", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtOperador.Text = "" Then
            MsgBox("Debes indicar el operador", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtTarjeta.Text = "" Then
            MsgBox("Debes indicar el codigo de la tarjeta", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub

        End If
        Dim IdTarjeta As Integer = BuscarIntDatoCitra("SELECT IdTarjeta FROM Tarjetas WHERE CodigoTarjeta='" & txtTarjeta.Text & "'")
        If IdTarjeta > 0 Then
            MsgBox("La tarjeta ya ha sido registrada con anterioridad", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim Tarjeta As New clsTarjetas
        Tarjeta.CodigoTarjeta = txtTarjeta.Text
        Tarjeta.IdOperador = BuscarIntDatoCitra("SELECT IdOperador FROM OperadoresGlobal WHERE Nombre='" & txtOperador.Text.Trim & "'")
        Tarjeta.InicioVigencia = datInicio.Value
        Tarjeta.TerminoVigencia = datTermino.Value
        Tarjeta.Status = 1
        Tarjeta.Codigo = BuscarStrDatoCitra("SELECT Codigo FROM OperadoresGlobal WHERE Nombre='" & txtOperador.Text.Trim & "'")
        Tarjeta.Insertar()
        Inicia()
        CargaTarjetas()
        Bitacora.RegistraActividad("Registró la tarjeta " & Tarjeta.CodigoTarjeta)
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Inicia()

    End Sub
End Class